package logic

import (
	"context"
	"gorm.io/gorm"
	"srv/models/mysql"

	"srv/internal/svc"
	"srv/sesame"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *sesame.GoodsAddRequest) (*sesame.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		Model:      gorm.Model{},
		GoodsTitle: in.GoodsTitle,
		GoodsStock: int(in.GoodsStock),
		GoodsCover: in.GoodsCover,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsSales: int(in.GoodsSales),
		Status:     int(in.Status),
	}
	err := g.Create()
	if err != nil {
		logx.Error("添加失败")
		return nil, err
	}
	return &sesame.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
